#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}开始生成用户数据...${NC}"

# 检查 Ignite 服务是否运行
if ! docker ps | grep -q "ignite-server.*Up"; then
    echo -e "${RED}错误: Ignite 服务未运行${NC}"
    exit 1
fi

# 复制 SQL 文件到容器
echo -e "${YELLOW}复制 SQL 文件到容器...${NC}"
docker cp scripts/generate-users.sql ignite-server:/opt/ignite/scripts/

# 执行 SQL 文件
echo -e "${YELLOW}执行 SQL 脚本...${NC}"
if docker exec ignite-server /opt/ignite/apache-ignite/bin/sqlline.sh -u "jdbc:ignite:thin://localhost:10800" -n ignite -p ignite123 -f /opt/ignite/scripts/generate-users.sql; then
    echo -e "${GREEN}用户数据生成成功！${NC}"
else
    echo -e "${RED}错误: 执行 SQL 脚本失败${NC}"
    exit 1
fi

# 验证数据
echo -e "${YELLOW}验证数据...${NC}"
docker exec ignite-server /opt/ignite/apache-ignite/bin/sqlline.sh -u "jdbc:ignite:thin://localhost:10800" -n ignite -p ignite123 -e "SELECT COUNT(*) FROM users" 